﻿using ALMDDD.Domain.Core.Events;
using ALMDDD.Domain.Core.Events.Interfaces;
using ALMDDD.Infrastructure.Repository.EventSourcing;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace ALMDDD.Application.EventSourcing
{
   public class SqlEventStoreService: IEventStoreService
    {
        // 注入我们的仓储接口
        private readonly IEventStoreRepository _eventStoreRepository;
        public SqlEventStoreService(IEventStoreRepository eventStoreRepository)
        {
            _eventStoreRepository = eventStoreRepository;

        }
        public void Save<T>(T thenEvent) where T : Event
        {
            // 对事件模型序列化
            var serializedData = JsonConvert.SerializeObject(thenEvent);

            var storedEvent = new StoredEvent(
                thenEvent,
                serializedData,
                "老郑");

            _eventStoreRepository.Store(storedEvent);
        }
    }
}
